<template>
  <div>
    <common-head title="商品列表" v-if="showNav"/>
    <!-- 商品排序栏 -->
    <div class="filter-bar" :style="{top:top}">
      <div :class="['sale','filter', {
        active: order.by === 'sale'
      }]"
        @click="bySale"
      >
        <span class="text">销量</span>
        <!-- 上三角 -->
        <div :class="['angle', 'up', {
          active:  order.by === 'sale' && order.updown === 1
        }]"></div>
        <!-- 下三角 -->
        <div :class="['angle', 'down', {
          active:  order.by === 'sale' && order.updown === 0
        }]"></div>
      </div>
      <div :class="['price','filter', {
        active: order.by === 'price'
      }]"
        @click="byPrice"
      >
        <span class="text">价格</span>
        <!-- 上三角 -->
        <div :class="['angle', 'up', {
          active:  order.by === 'price' && order.updown === 1
        }]"></div>
        <!-- 下三角 -->
        <div :class="['angle', 'down', {
          active:  order.by === 'price' && order.updown === 0
        }]"></div>
      </div>
    </div>
    <!-- 商品排序栏 -->
    <!-- 商品列表 -->
    <div class="wrap">
      <div class="items">
      <div class="item" v-for="item in items" :key="item.id">
        <div class="item-left">
          <img :src="item.pic" alt="">
        </div>
        <div class="item-right">
          <h4 class="name">{{item.name}}</h4>
          <p class="desc">
            {{ item.characteristic ? item.characteristic :'暂无描述' }}
          </p>
          <div class="action">
            <span class="price">
              ￥ {{item.minPrice}}
            </span>
            <van-icon color="#f66" size="24" class-prefix="icon" name="jia-tianchong
"/>
          </div>
        </div>
      </div>
    </div>
    </div>
    <!-- /商品列表 -->
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { fetchItems } from '@api'
export default {
  data () {
    return {
      top: '46px',
      showNav: true,
      order: {
        by: 'sale', // sale按照销量排序 price 按照价格排序
        updown: 0 // 1升序 0 降序
      },
      items: [],
      // 请求商品排序规则
      orderBy: 'ordersDown'
    }
  },
  mounted () {
    this.navScroll()
  },
  created () {
    this.fetchItems()
  },
  methods: {
    fetchItems () {
      // 请求当前分类下商品
      fetchItems({
        categoryId: this.$route.params.cateId,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    navScroll () {
      // 控制滚动条滚动 时导航
      window.onscroll = () => {
        // 获取滚动举例
        const sTop = document.documentElement.scrollTop || document.body.scrollTop
        if (sTop > 46) {
          this.showNav = false
          this.top = 0
        } else {
          this.showNav = true
          this.top = '46px'
        }
      }
    },
    bySale () {
      // 点击按照销量排序
      // 情况1 此时按照价格排序，点击按照销量
      /*
      默认按照销量的 降序排序
      */
      if (this.order.by === 'price') {
        this.order.by = 'sale'
        this.order.updown = 0
        this.orderBy = 'ordersDown'
      } else {
        // 情况2  此时就是 按照销量排序 切换升序和降序
        if (this.order.updown === 0) {
          this.order.updown = 1
          this.orderBy = 'ordersUp'
        } else {
          this.order.updown = 0
          this.orderBy = 'ordersDown'
        }
      }

      this.fetchItems()
    },
    byPrice () {
      // 点击按照价格排序
      // 情况1 此时按照销量排序，点击按照价格
      /*
      默认按照价格的 升序排序
      */
      if (this.order.by === 'sale') {
        this.order.by = 'price'
        this.order.updown = 1
        this.orderBy = 'priceUp'
      } else {
        // 情况2  此时就是 按照销量排序 切换升序和降序
        if (this.order.updown === 0) {
          this.order.updown = 1
          this.orderBy = 'priceUp'
        } else {
          this.order.updown = 0
          this.orderBy = 'priceDown'
        }
      }
      this.fetchItems()
    }
  },
  components: {
    CommonHead
  },
  beforeDestroy () {
    // 注销全局事件绑定
    window.onscroll = null
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  margin-top: 86px;
}
.filter-bar{
  height:40px;
  width: 100%;
  background-color: #fafafa;
  position: fixed;
  left:0;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #333;
  justify-content: space-around;
  .filter{
    &.active{
      color:#f66;
    }
    position: relative;
    .angle{
      width:0;
      height:0;
      border: 6px solid transparent;
      position: absolute;
      right: -16px;
      &.up{
        border-bottom-color:#333;
        top: -3px;
        &.active{
          border-bottom-color:#f66;
        }
      }
      &.down{
        border-top-color:#333;
        top:11px;
        &.active{
          border-top-color:#f66;
        }
      }
    }
  }
}
.item{
  height: 180px;
  display: flex;
  align-items: center;
  &-left{
    height: 180px;
    img{
      width: 180px;
      height: 180px;

    }
  }
  &-right{
    flex:1;
    height: 180px;
    padding:0 10px;
    box-sizing: border-box;
    .name{
      font-weight: 100;
      font-size: 16px;
      line-height: 40px;
      color:#333;
    }
    .desc{
      line-height: 36px;
      font-size: 14px;
      color: #666;
    }
    .action{
      display: flex;
      justify-content: space-between;
      padding-right: 20px;
      box-sizing: border-box;
      align-items: center;
      .price{
        color: #ff2222;

      }
    }
  }
}
</style>
